﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JCTimes.DomainModel.Repository;
using System.Web.Mvc;
using JCTimes.DomainModel.Entity;
using JCTimes.Portal.Models;

namespace JCTimes.Portal.Infrastructure
{
    //the types of dropdownlist
    public enum DropDownListType
    {
        Empty = 0,
        Agency,
        Account,
        ConnectFlag,
        PdaStatus,
        PDAFunctionTree
    }
    /// <summary>
    /// Manage all dropdownlist used in the system.
    /// </summary>
    public class DropDownList : List<SelectListItem>
    {
        private IViewRepository vRep = (IViewRepository)DependencyResolver.Current.GetService(typeof(IViewRepository));
        private List<SelectListItem> treelist = new List<SelectListItem>();
        private int treecount = 0;


        private IEnumerable<SelectListItem> ConnectFlag = new List<SelectListItem>(){
                new SelectListItem { Text="合法", Value="合法"},
                new SelectListItem { Text="非法", Value="非法"}
        };

        private IEnumerable<SelectListItem> PdaStatus = new List<SelectListItem>(){
                new SelectListItem { Text="未授权", Value="0"},
                new SelectListItem { Text="已授权", Value="1"}
        };

        /// <summary>
        /// Initialize the specified dropdownlist instance.
        /// </summary>
        /// <param name="ddlType">dropdownlist type</param>
        public DropDownList(DropDownListType ddlType)
        {
            GetItems(ddlType);
        }

        /// <summary>
        /// Add list items to the specified dropdownlist.
        /// </summary>
        /// <param name="ddlType">type of dropdownlist</param>
        private void GetItems(DropDownListType ddlType)
        {
            //服务器缓存的时间
            int cacheTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CacheMinute"] + "");
            UserInfo uinfo = SessionHelper.CurrUser;

            switch (ddlType)
            {
                case DropDownListType.Agency:
                    List<User> UserInfo = vRep.GetList<User>().Where(m => m.Flag == 0 && m.RoleID == Constant.Agency).ToList();
                    this.AddRange(UserInfo.Select(r => new SelectListItem { Value = r.ID, Text = r.Name }).Distinct());
                    break;
                case DropDownListType.Account:
                    List<User> UserInfo1 = vRep.GetList<User>().Where(m => m.Flag == 0 && m.RoleID == Constant.Account).ToList();
                    if (uinfo.RoleID == Constant.Agency)
                    {
                        UserInfo1 = vRep.GetList<User>().Where(m => m.Flag == 0 && m.RoleID == Constant.Account&&m.ParentID==uinfo.UserID).ToList();
                    }
                    this.AddRange(UserInfo1.Select(r => new SelectListItem { Value = r.ID, Text = r.Name }).Distinct());
                    break;
                case DropDownListType.PDAFunctionTree:
                    this.AddRange(GetPDAFunctionTree());
                    break;
                case DropDownListType.ConnectFlag:
                    this.AddRange(ConnectFlag);
                    break;
                case DropDownListType.PdaStatus:
                    this.AddRange(PdaStatus);
                    break;
                case DropDownListType.Empty:
                default:
                    break;
            }
        }

        /// <summary>
        /// Initialize menu list items of menu dropdownlist. 
        /// </summary>
        private List<SelectListItem> GetPDAFunctionTree()
        {
            List<SelectListItem> itemlist = new List<SelectListItem>();
            List<PDAFunction> FunctionInfo = vRep.GetList<PDAFunction>().ToList();

            var pdaFunctions = FunctionInfo.OrderBy(f => f.OrderID).ToList();
            itemlist.Add(new SelectListItem { Value = "0", Text = "无" });

            foreach (var item in pdaFunctions.Where(f => f.ParentID == null || f.ParentID == "0"))
            {
                itemlist.Add(new SelectListItem { Value = item.ID, Text = item.FunctionName });
                treelist = new List<SelectListItem>();
                treecount = 0;
                List<SelectListItem> getTreeList= GetPDAFunctionTreeTraversal(item.ID);
                foreach (var itemtree in getTreeList)
                {
                    itemlist.Add(itemtree);
                }
            }
            return itemlist;
        }

        private List<SelectListItem> GetPDAFunctionTreeTraversal(string parentid)
        {
            List<PDAFunction> FunctionInfo = vRep.GetList<PDAFunction>().ToList();
            var functionlist = FunctionInfo.Where(m => m.ParentID == parentid).OrderBy(m => m.OrderID).ToList();
            if (functionlist != null && functionlist.Count() > 0)
            {
                treecount++;
            }
            else
            {
                treecount = 0;
            }
            
            foreach (var item in functionlist)
            {
                string strPrex = "--";
                if (treecount == 2)
                {
                    strPrex = strPrex + strPrex;
                }
                else if (treecount == 3)
                {
                    strPrex = strPrex + strPrex + strPrex;
                }
                else if (treecount == 4)
                {
                    strPrex = strPrex + strPrex + strPrex + strPrex;
                }
                else if (treecount == 5)
                {
                    strPrex = strPrex + strPrex + strPrex + strPrex + strPrex;
                }
                else if (treecount == 6)
                {
                    strPrex = strPrex + strPrex + strPrex + strPrex + strPrex + strPrex;
                }
                treelist.Add(new SelectListItem { Value = item.ID, Text = strPrex + item.FunctionName });
                GetPDAFunctionTreeTraversal(item.ID);
            }
            
            return treelist;
        }

        /// <summary>
        /// Listitem comparison class
        /// </summary>
        public class SelectListComparor : IEqualityComparer<SelectListItem>
        {
            public bool Equals(SelectListItem x, SelectListItem y)
            {
                return x.Value == y.Value
                        && x.Text == y.Text;
            }

            public int GetHashCode(SelectListItem obj)
            {
                return obj.Value.GetHashCode() + obj.Text.GetHashCode();
            }
        }

    }
}
